package com.zc.controller.oldStore;

import com.zc.dto.ResultDto;
import com.zc.entity.Path;
import com.zc.service.permissionService.PathService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 路径控制器
 */
@CrossOrigin
@RequestMapping("/path")
@Controller
public class PathController extends BaseController {
    @Resource
    private PathService pathService;

    @RequestMapping("/preAddPath")
    public String preAddPath() {
        return "addPath";
    }

    @RequestMapping(value = "/addPath", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto saveOrUpdate(Path path) {
        ResultDto resultDto = new ResultDto();
        try {
            pathService.saveOrUpdatePath(path);
            resultDto.setMessage("success");
            resultDto.setSuccess(true);
            resultDto.setStatus(0);
        } catch (Exception e) {
            resultDto.setMessage("path saveOrUpdate error");
            resultDto.setSuccess(false);
            resultDto.setStatus(1);
            LOG.error("path saveOrUpdate error,{}", e);
        }
        return resultDto;
    }

    @RequestMapping(value = "/findFatherLeaf",method = RequestMethod.POST)
    @ResponseBody
    public ResultDto findFatherLeaf(Integer fatherLeaf){
        ResultDto result = new ResultDto();
        try {
            List<Path> pathList= pathService.findFatherLeaf(fatherLeaf);
            result.setStatus(0);
            result.setSuccess(true);
            result.setMessage("拉取父节点信息成功");
            result.setList(pathList);
        } catch (Exception e) {
            LOG.error("findFatherLeaf error::,{}",e);
            result.setResultData(false,1,"拉取父类信息失败",null);
        }
        return result;
    }

}
